package simjistwrapper.xml;

import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import simjistwrapper.exceptions.ParsingException;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/*
 * Created on Oct 6, 2005
 */
public class ParserSimFile
{
    public ParserSimFile()
    {}

    public Document parse(File file) throws ParsingException
    {
        Document fileParser = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        factory.setIgnoringElementContentWhitespace(true);
        try
        {
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setErrorHandler(new ErrorHandler() {
                public void fatalError(SAXParseException exception)
                        throws SAXException
                {
                    throw exception;
                	//new ParsingException(null, exception);
                }

                public void error(SAXParseException e) throws SAXParseException
                {
                    throw e;
                	//new ParsingException(null, e);
                }

                public void warning(SAXParseException err)
                        throws SAXParseException
                {
                    System.out.println("** Warning" + ", line "
                            + err.getLineNumber() + ", uri "
                            + err.getSystemId());
                    System.out.println("   " + err.getMessage());
                }
            });
            fileParser = builder.parse(file);
        } catch(SAXParseException spe)
        {
        	String msg = "\n** Parsing error" + ", line " + spe.getLineNumber()
					+ ", uri " + spe.getSystemId() + "\n   " + spe.getMessage();
        	throw new ParsingException(msg, spe);
        	/*
            // Error generated by the parser
            System.out.println("\n** Parsing error" + ", line "
                    + spe.getLineNumber() + ", uri " + spe.getSystemId());
            System.out.println("   " + spe.getMessage());
            System.exit(-1);
            */
        } catch(SAXException sxe)
        {
        	throw new ParsingException("Error generated during parsing", sxe);
        	/*
            // Error generated during parsing)
            Exception x = sxe;
            if(sxe.getException() != null)
                x = sxe.getException();
            x.printStackTrace();
            System.exit(-1);
            */
        } catch(ParserConfigurationException pce)
        {
        	/*
            // ParserOld with specified options can't be built
            pce.printStackTrace();
            System.exit(-1);
            */
        	throw new ParsingException(
					"ParserOld with specified options can't be built", pce);
        } catch(IOException ioe)
        {
        	/*
            // I/O error
            ioe.printStackTrace();
            System.exit(-1);
            */
        	throw new ParsingException("I/O Error during parsing", ioe);
        }
        return fileParser;
    }
}
